package com.lovo.view.school;

import com.lovo.bean.SchoolBean;
import com.lovo.javafxComponent.LovoTextArea;
import com.lovo.javafxComponent.LovoTitle;
import com.lovo.javafxComponent.LovoTxt;
import com.lovo.service.ISchoolService;
import com.lovo.service.impl.SchoolServiceImpl;

import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.layout.Pane;


/**
 * 添加学校信息
 * @author lovo
 *
 */
public class AddSchoolDialog extends Dialog{
    /**内容容器*/
    private Pane container = new Pane();

    /**学校名称文本框*/
    private LovoTxt nameTxt = new LovoTxt("学校名称", 80, 100,container);
    /**校长文本框*/
    private LovoTxt leaderTxt = new LovoTxt("校长", 80, 150,container);
    /**地址文本框*/
    private LovoTxt addrTxt = new LovoTxt("学校地址", 80, 200,container);
    /**学生人数文本框*/
    private LovoTxt studentCountTxt = new LovoTxt("学生人数", 80, 250,container);
    /**老师人数文本框*/
    private LovoTxt teacherCountTxt = new LovoTxt("老师人数", 80, 300,container);
    /**联系电话文本框*/
    private LovoTxt phoneTxt = new LovoTxt("联系电话", 80, 350,container);
    /**说明文本框*/
    private LovoTextArea infoArea = new LovoTextArea("说明", 80, 400,container);
    /**学校主面板*/
    private SchoolPane schoolPane;
    private ISchoolService schoolService = new SchoolServiceImpl();
    /**
     * 构造方法
     */
    public AddSchoolDialog(SchoolPane schoolPane) {
        this.schoolPane = schoolPane;
        this.setTitle("添加学校");
        initComponent();
    }

    /**
     * 初始化组件
     */
    private void initComponent() {
        new LovoTitle("添加学校", -150, 30, container);
        container.setPrefSize(480, 500);
        this.getDialogPane().setContent(container);

        this.initInfo();


        ButtonType addButtonType = new ButtonType("添加", ButtonData.YES);
        this.getDialogPane().getButtonTypes().add(addButtonType);
        Button btnLogin = (Button) this.getDialogPane().lookupButton(addButtonType);

        btnLogin.addEventFilter(ActionEvent.ACTION, event -> {
            if(addStudent() == false) {
                event.consume();
            }
        });

        ButtonType cancelButtonType = new ButtonType("取消", ButtonData.CANCEL_CLOSE);
        this.getDialogPane().getButtonTypes().add(cancelButtonType);

        this.showAndWait();
    }

    //------------------------

    /**
     * 添加
     * @return 添加是否成功
     */
    private boolean addStudent() {
        if(check() == false){
            return false;
        }

        SchoolBean schoolBean = new SchoolBean();
        schoolBean.setName(nameTxt.getText());
        schoolBean.setLeader(leaderTxt.getText());
        schoolBean.setAddr(addrTxt.getText());
        schoolBean.setStudentCount(Integer.parseInt(studentCountTxt.getText()));
        schoolBean.setTeacherCount(Integer.parseInt(teacherCountTxt.getText()));
        schoolBean.setPhone(phoneTxt.getText());
        schoolBean.setInfo(infoArea.getText());
        schoolBean.setStatus("接洽中");
        schoolBean.setCity("成都");
        schoolService.add(schoolBean);
        schoolPane.findSchoolInfo(1);
        return true;
    }

    private boolean check(){
        return  true;
    }

    /**
     * 初始化下拉框信息
     */
    private void initInfo() {

    }

}
